package com.fsnbigdata.repository;

import com.querydsl.core.types.Predicate;
import io.springlets.data.jpa.repository.support.QueryDslRepositorySupportExt;
import org.springframework.roo.addon.layers.repository.jpa.annotations.RooJpaRepositoryCustomImpl;
import com.fsnbigdata.domain.Park;
import com.fsnbigdata.domain.QPark;
import com.querydsl.core.types.Path;
import com.querydsl.jpa.JPQLQuery;
import io.springlets.data.domain.GlobalSearch;

import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;

/**
 * = ParkRepositoryImpl
 *
 * TODO Auto-generated class documentation
 *
 */
@RooJpaRepositoryCustomImpl(repository = ParkRepositoryCustom.class)
@Transactional(readOnly = true)
public class ParkRepositoryImpl extends QueryDslRepositorySupportExt<Park> implements ParkRepositoryCustom {

    /**
     * TODO Auto-generated constructor documentation
     */
    ParkRepositoryImpl() {
        super(Park.class);
    }

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String IMAGE = "image";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String DESCRIPTION = "description";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String CREATOR = "creator";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String NAME = "name";

    /**
     * 类型
     */
    public static final String TYPE = "type";

    @Override
    public Page<Park> findAll(Predicate predicate, Pageable pageable) {
        QPark park = QPark.park;
        JPQLQuery<Park> query = from(park).where(predicate)
                .offset(pageable.getOffset())
                .limit(pageable.getPageSize())
                .fetchAll();
        applyPagination(pageable, query);
        return loadPage(query, pageable, park);
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param globalSearch
     * @param pageable
     * @return Page
     */
    public Page<Park> findAll(GlobalSearch globalSearch, Pageable pageable, String creator) {
        QPark park = QPark.park;
        JPQLQuery<Park> query = from(park);
        if(creator != null ){
            query.where(park.creator.eq(creator));
        }
        Path<?>[] paths = new Path<?>[] { park.name, park.description, park.image, park.creator,park.type };
        applyGlobalSearch(globalSearch, query, paths);
        AttributeMappingBuilder mapping = buildMapper().map(NAME, park.name).map(DESCRIPTION, park.description).map(IMAGE, park.image).map(CREATOR, park.creator).map(TYPE,park.type);
        applyPagination(pageable, query, mapping);
        applyOrderById(query);
        return loadPage(query, pageable, park);
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param ids
     * @param globalSearch
     * @param pageable
     * @return Page
     */
    public Page<Park> findAllByIdsIn(List<Long> ids, GlobalSearch globalSearch, Pageable pageable) {
        QPark park = QPark.park;
        JPQLQuery<Park> query = from(park);
        Path<?>[] paths = new Path<?>[] { park.name, park.description, park.image, park.creator,park.type };
        applyGlobalSearch(globalSearch, query, paths);
        // Also, filter by the provided ids
        query.where(park.id.in(ids));
        AttributeMappingBuilder mapping = buildMapper().map(NAME, park.name).map(DESCRIPTION, park.description).map(IMAGE, park.image).map(CREATOR, park.creator).map(TYPE,park.type);
        applyPagination(pageable, query, mapping);
        applyOrderById(query);
        return loadPage(query, pageable, park);
    }

    @Override
    public Page<Park> findAll(Pageable pageable) {
        QPark park = QPark.park;
        JPQLQuery<Park> query = from(park)
                .offset(pageable.getOffset())
                .limit(pageable.getPageSize())
                .fetchAll();
        return loadPage(query, pageable,park);
    }
}
